﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.AspNet.Identity;
using TCC.CMFlow.Contexts;
using TCC.CMFlow.Models;

namespace TCC.CMFlow.Controllers
{
    public class AuthorizeUserAttribute : AuthorizeAttribute
    {
        public eModulosSistema Modulo { get; set; }
        public ePermissoes Permissao { get; set; }
        public bool isModal { get; set; }

        
        private DataContext db = new DataContext();

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var isAuthorized = base.AuthorizeCore(httpContext);
            if (!isAuthorized)
            {
                return false;
            }

            if (Modulo == 0 && Permissao == 0)
                return true;

            var userID = httpContext.User.Identity.GetUserId();
            var usuario = db.Users.Find(userID);
            var hasPermissao = HasPerfilAcessoPermissao(usuario.PerfilAcesso.ID);
            
            if (!hasPermissao)
            {
                return false;
            }

            return true;
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary(
                        new
                        {
                            Controller = "Account",
                            Action = "Login"
                        }
                    )
                );
            }
            else
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary(
                        new
                        {
                            Controller = "Access",
                            Action = (isModal ? "_Denied" : "Denied")
                        }
                    )
                );
            }
        }

        private bool HasPerfilAcessoPermissao(int perfilAcessoID)
        {
            var perfilacessoconfiguracao = (from p in db.PerfilAcessoConfiguracao
                                            where p.PerfilAcessoID == perfilAcessoID
                                            select p)
                                           .Where(PerfilAcessoConfiguracao.IsPerfilAtivo)
                                           .ToList();

            return (from c in perfilacessoconfiguracao
                    where c != null &&
                          c.PerfilAcesso.ID == perfilAcessoID &&
                          c.ModuloSistema.ID == (int)this.Modulo &&
                          c.Permissoes.Any(p => (int)this.Permissao == 0 || p.ID == (int)this.Permissao)
                    select c).Any();
        }

    }
}